/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **
 **  File Name:      ProcessLogHandler.java
 **  Description:    
 **  Date:           2008-12-4
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.workflow.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.def.Transition;
import org.jbpm.graph.exe.Comment;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.ti.andon.persistent.JbpmAdProcessLog;
import com.ti.andon.workflow.WFConstants;

public class ProcessLogHandler implements ActionHandler
{
	/**
	 * Serial Version Id.
	 */
	private static final long serialVersionUID = 8634405007453570010L;

	private static Log logger = LogFactory.getLog(ProcessLogHandler.class);
	
	/* (non-Javadoc)
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext executionContext) throws Exception
	{
		try
		{
			TaskInstance ti = executionContext.getTaskInstance();
			ContextInstance context = executionContext.getContextInstance();
			if (null != context.getVariable(WFConstants.TASK_PROCESSED)
					&& context.getVariable(WFConstants.TASK_PROCESSED).equals(WFConstants.STRING_YES))
			{
				context.setVariable(WFConstants.TASK_PROCESSED, null);

				JbpmAdProcessLog jbpmAdProcessLog = new JbpmAdProcessLog();
				jbpmAdProcessLog.setProcessInstanceId(new Long(executionContext.getProcessInstance().getId()));
				if (null != executionContext.getProcessInstance().getKey())
				{
					jbpmAdProcessLog.setBusinessKey(executionContext.getProcessInstance().getKey());
				}

				jbpmAdProcessLog.setStatus(ti.getTask().getDescription());
				
				Long departmentId = null;
//				if (null != context.getVariable(WFConstants.BUDGET_RES_DEPARTMENT))
//				{
//					departmentId = (Long) context.getVariable(WFConstants.BUDGET_RES_DEPARTMENT);
//				}
//				else
//				{
//					departmentId = WFConstants.FIN_DEPARTMENT_ID;
//				}
				jbpmAdProcessLog.setDepartmentId(departmentId);
				
				String currTransitionName = (String)context.getVariable(WFConstants.CURRENT_TRANSITION_NAME);
				if (null != currTransitionName)
				{
					Transition leavingTransition = executionContext.getNode().getLeavingTransition(
							(String) context.getVariable(WFConstants.CURRENT_TRANSITION_NAME));
					if (null != leavingTransition)
					{
						jbpmAdProcessLog.setProcessType(leavingTransition.getDescription());
					}
				}
				
				jbpmAdProcessLog.setProcessTime(ti.getEnd());

				List comments = ti.getComments();
				if (comments != null && comments.size() > 0)
				{
					jbpmAdProcessLog.setComments(((Comment) ti.getComments().get(0)).getMessage());
				}

				jbpmAdProcessLog.setProcessName((String) context.getVariable(WFConstants.PROCESS_NAME));
				if (null != ti.getActorId())
				{
					jbpmAdProcessLog.setUserId(Long.valueOf(ti.getActorId()));
				}
				
				executionContext.getJbpmContext().getSession().save(jbpmAdProcessLog);
			}
		}
		catch (Exception e)
		{
			logger.error("ProcessLogHandler.execute() : Error occurred : " + e.toString());
			e.printStackTrace();
			throw new Exception(e.getMessage(), e);
		}
	}
}
